#ifndef __GLF_TASK_TASKHANDLER_DECL_H_INCLUDED__
#define __GLF_TASK_TASKHANDLER_DECL_H_INCLUDED__

#include <glf/core/threadTraits.h>
#include <glf/task/taskHandlerConsumePolicy_decl.h>

namespace glf {

class TaskManager;
class TaskCondition;

class TaskHandlerBase : NonCopyable
{
public:
	virtual ~TaskHandlerBase()
	{
	}

	virtual bool Consume() = 0;
	virtual bool IsEquivalent(TaskHandlerBase* ) = 0;
	virtual const void* GetTaskManagerPtr() = 0;
};
	
template<typename Tag>
class TaskHandlerImpl : public TaskHandlerBase
{
private:

	class Working
	{
	public:
		Working(TaskManager& tm);
		~Working();

	private:
		TaskManager& m_Tm;
	};

public:

	explicit TaskHandlerImpl(TaskCondition* tc = NULL);
	~TaskHandlerImpl();
	
	// Deprecated ?
	bool IsAttachedToThread();

	// Deprecated ?
	void AttachToThread();

	// Deprecated ?
	void DetachFromThread();

	bool IsEquivalent(TaskHandlerBase* th);

	void Register();

	void Unregister();

protected:
	const void* GetTaskManagerPtr();

	TaskCondition* m_TaskCondition;
	ThreadId m_AttachedToThread; 	// Deprecated ?
	ThreadId m_RegisteredThreadId;

};

template<typename Tag>
class TaskHandler : public TaskHandlerImpl<Tag>, public TaskHandlerConsumePolicy<Tag>
{
public:

	explicit TaskHandler(TaskCondition* tc = NULL);

	bool Consume();
};

} // end namespace glf


#endif // __GLF_TASK_TASKHANDLER_DECL_H_INCLUDED__
